package com.itheima.listviewsample;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ListViewGridViewAdapter extends BaseAdapter {
    private Map<String, List<String>> data;
    private List<String> keys;
    private Context mContext;

    public ListViewGridViewAdapter(Context mContext, Map<String, List<String>> data) {
        this.mContext = mContext;
        this.data = data;
        keys = new ArrayList<>();
        keys.addAll(data.keySet());
    }

    @Override
    public int getCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.listview_gridview_item, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        String key = keys.get(position);
        viewHolder.textView.setText(key);
        viewHolder.gridView.setAdapter(new GridAdapter(mContext, data.get(key)));


        return convertView;
    }


    public static class ViewHolder {
        public TextView textView;
        public GridView gridView;

        public ViewHolder(View view) {
            textView = (TextView) view.findViewById(R.id.tv);
            gridView = (GridView) view.findViewById(R.id.gv);
        }
    }
}